package swle.xyz.austers.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import swle.xyz.austers.R;

public class NewsDetailsActivity extends BaseActivity {

    TextView textViewTitle;
    TextView textViewTime;
    TextView textViewMainText;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_details);
        handler = new Handler();
        initView();
        initEvent();
    }

    @Override
    public void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar_NewsDetailsActivity);
        setSupportActionBar(toolbar); //将toolbar设置为当前activity的操作栏
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//添加默认的返回图标
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏toolbar默认显示的label

        textViewTitle = findViewById(R.id.textView_newsTitleNewsDetailsActivity);
        textViewTime = findViewById(R.id.textView_newsTime_NewsDetailsActivity);
        textViewMainText = findViewById(R.id.textView_newsMainText_NewsDetailsActivity);
    }

    @Override
    public void initEvent() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Document doc = null;
                try {
                    Intent intent =getIntent();
                    String url = intent.getStringExtra("url");
//                    System.out.println("*********************"+url);
                    final String title = intent.getStringExtra("title");
                    url = "http://news.aust.edu.cn/"+url;
                    doc = Jsoup.connect(url).get();
                    Elements info = doc.getElementsByClass("list-infor");
                    Elements imggin = doc.getElementsByClass("imggin");
//
//                    System.out.println(imggin);
//                    System.out.println("===============");
                    final Elements elements = imggin.select("p");
//                    System.out.println(elements.size());

                    String time = info.text().replace(" ","");
                    time = time.substring(time.indexOf("发布日期：")+5,time.indexOf("】"));
//                    System.out.println(title);
                    final String finalTime = time;
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            textViewTime.setText(finalTime);
                            textViewTitle.setText(title);
                            for (Element element : elements){
//                                System.out.println(element.text());
                                if (!element.text().equals("")){
                                    textViewMainText.append(element.text());
                                    textViewMainText.append("\n\n");
                                }

                            }
                        }
                    };
                    handler.post(runnable);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}